package com.swfee.qinben.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.swfee.qinben.QinBenActivity;
import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.session.MyApplication;

/**
 * @author ddr
 * 
 */
public class CurenActivitUtils {

	private static AppBaseActivity ctx = null;

	public static void setCurActivity(AppBaseActivity activity) {
		ctx = activity;
	}

	public static AppBaseActivity getCurActivity() {
		return ctx;
	}

	public static void setHeaderTitle(String str, int index) {

		QinBenActivity qin = null;
		if (ctx.getParent() instanceof QinBenActivity) {
			qin = (QinBenActivity) ctx.getParent();

		} else if (ctx instanceof QinBenActivity) {
			qin = (QinBenActivity) ctx;

		}
		if (qin != null)
			qin.setHeaderTitle(str, index);
	}

	public static void tabIndexToIntent(int index, Activity curActivity,
			Class class1, Bundle params) {

		QinBenActivity qinBenActivity = (QinBenActivity) curActivity
				.getParent();

		String className = class1.getName();
		Class c = null;
		try {
			c = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		qinBenActivity.reLoadViews(index, class1, params);
	}

	public static void tabIndexToIntent(int index, Activity curActivity,
			Class class1) {

		tabIndexToIntent(index, curActivity, class1, null);

	}

	public static void toTabIndexToIntent(int index) {

		QinBenActivity qin = null;
		if (ctx.getParent() instanceof QinBenActivity) {
			qin = (QinBenActivity) ctx.getParent();

		} else if (ctx instanceof QinBenActivity) {
			qin = (QinBenActivity) ctx;

		}
		if (qin != null)
			qin.pager.setCurrentItem(index);

	}

	//

	public static void setSpinnerByArray(int spinnerId, String[] array) {

		Spinner spinner = (Spinner) ctx.findViewById(spinnerId);
		ArrayAdapter<String> ad = new ArrayAdapter<String>(ctx,
				android.R.layout.simple_spinner_item, array);
		spinner.setAdapter(ad);
		// spinner.setBackgroundColor(0x00000000);
	}

	public static void setTextViewContent(int txtId, String text, View parent) {

		setTextViewContent(txtId, text, parent, null);

	}

	public static void setTextViewContent(int txtId, String text, View parent,
			Integer colorId) {

		TextView txtv = (TextView) parent.findViewById(txtId);
		txtv.setText(text);
		if (colorId != null) {
			txtv.setTextColor(getCurActivity().getResources().getColor(colorId));
		}
	}

	public static void setImageView(int imgId, int imgSrcId) {

		ImageView iv = (ImageView) ctx.findViewById(imgId);
		iv.setImageDrawable(ctx.getResources().getDrawable(imgSrcId));
	}

	public static void startActivity(Class c, boolean closeCur) {
		startActivity(c, closeCur, null);
	}

	public static void startActivity(Class c, boolean closeCur, Bundle bundle) {
		Intent intent = new Intent(CurenActivitUtils.getCurActivity(), c);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		if (closeCur) {
			CurenActivitUtils.getCurActivity().startActivityBeforeFinish(intent);
		} else {
			CurenActivitUtils.getCurActivity().startActivityUndoFinish(intent);
		}
	}

	public static void exitDialog() {

		new AlertDialog.Builder(ctx).setTitle("退出亲本").setMessage("确定退出吗?")
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener()

				{
					public void onClick(DialogInterface dialog, int whichButton) {

						MyApplication.getInstance().exit();
					}
				}).show();
	}

}
